package com.GroupSix.service;

import com.GroupSix.dao.PracticeDao;
import com.GroupSix.dao.QuestionBankDao;
import com.GroupSix.entity.PracticeEntity;
import com.GroupSix.entity.QuestionBankEntity;
import com.GroupSix.entity.StudentEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service("PracticeService")
public class PracticeServiceImpl extends ServiceImpl<PracticeDao, PracticeEntity>
        implements PracticeService {
    @Autowired
    private PracticeDao practiceDao;
    @Autowired
    private QuestionBankDao questionBankDao;
    @Autowired
    private StudentService studentService;

    //增
    @Override
    public Integer add(PracticeEntity practiceEntity) {
        practiceEntity.setCreateTime(LocalDateTime.now());
        practiceEntity.setUpdateTime(LocalDateTime.now());
        return practiceDao.insert(practiceEntity);
    }

    //删
    @Override
    public Integer delete(int id) {
        return practiceDao.deleteById(id);
    }

    //查
    @Override
    public PracticeEntity find(int id) {
        return practiceDao.selectById(id);
    }

    //改
    @Override
    public Integer update(PracticeEntity practiceEntity) {
        practiceEntity.setUpdateTime(LocalDateTime.now());
        return practiceDao.updateById(practiceEntity);
    }

    //根据科目生成训练题
    //形参:科目,题目数量,学号
    @Override
    public PracticeEntity generatePractice(String courseName, Integer number, Integer studentId) {
        //根据学号定位到学生
        StudentEntity student = studentService.getByStudentId(studentId);
        Integer ability = student.getAbilityLevel();
        //如果之前未做过题，能力默认为1
        if (ability == 0) {
            ability = 1;
        }
        //根据科目和学生能力进行筛选
        QueryWrapper<QuestionBankEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("qscourse", courseName)
                .eq("qsdifficulty", ability);

        //随机选择所需数量的题目
        List<QuestionBankEntity> questions = questionBankDao.selectList(queryWrapper);
        if (questions.size() > number) {
            Collections.shuffle(questions);
            questions = questions.subList(0, number);
        } else {
            throw new RuntimeException("题目数量不足！！！需要：" + number + "实际：" + questions.size());
        }

        //生成题号数组
        Integer[] array = new Integer[questions.size()];
        for (int i = 0; i < questions.size(); i++) {
            QuestionBankEntity questionBankEntity = questions.get(i);
            long temp = questionBankEntity.getId();
            array[i] = (int) temp;
        }

        //返回训练题信息
        PracticeEntity practiceEntity = new PracticeEntity();
        practiceEntity.setCourseName(courseName);
        practiceEntity.setQuestionSum(number);
        practiceEntity.setDifficultyLevel(ability);
        practiceEntity.setQuestions(array);
        practiceEntity.setCreateTime(LocalDateTime.now());
        practiceEntity.setUpdateTime(LocalDateTime.now());
        practiceEntity.setIsActive(0);
        //保存训练题信息
        practiceDao.insert(practiceEntity);

        return practiceEntity;
    }

    //根据正确率实时修改学生能力等级
    //形参：练习题id，学号，答题情况
    @Override
    public Integer autoAssessment(Long practiceId, Integer studentId, String[] answerCard) {
        //获取练习题信息
        Integer[] questions = practiceDao.selectById(practiceId).getQuestions();
        Integer difficulty = practiceDao.selectById(practiceId).getDifficultyLevel();

        //根据答题情况判断正确率
        int num = questions.length;//记录题目总数
        int count = 0;             //记录正确题目的数量
        for (int i = 0; i < num; i++) {
            String str = questionBankDao.selectById(questions[i]).getQsanswer();
            if (str.equals(answerCard[i])) {
                count++;
            }
        }
        double rightRate = count / (double) num;//计算正确率
        //用于测试输出
        //System.out.println("正确率为："+rightRate);

        //根据学号定位到学生
        StudentEntity student = studentService.getByStudentId(studentId);
        Integer ability = student.getAbilityLevel();
        if (ability == 0) {
            ability = 1;
        }
        //根据练习题难度和正确率修改能力等级
        //正确率大于90%能力提高一级，正确率低于30%能力降一级
        //能力分为1、2、3共三个等级
        if (rightRate > 0.9 && difficulty >= ability && difficulty < 3) {
            student.setAbilityLevel(difficulty + 1);
        } else if (rightRate < 0.3 && difficulty <= ability && difficulty > 1) {
            student.setAbilityLevel(difficulty - 1);
        }

        //用于测试输出
        //System.out.println("修改后的能力等级为："+student.getAbilityLevel());

        //保存修改后的信息
        return studentService.update(student);
    }
}
